package com.shop.shoporder.wapController;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.shoporder.common.*;


import com.shop.shoporder.entity.*;
import com.shop.shoporder.service.GoodsCollectService;
import com.shop.shoporder.service.WapGoodsAndSpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/wap")
public class WapGoodsAndSpecController {


    @Autowired
    WapGoodsAndSpecService wapGoodsAndSpecService;


    @Autowired
    GoodsCollectService goodsCollectService;

    @Autowired
    RedisTemplate redisTemplate;

//    /**
//     * 根据首页商品专区查询商品
//     *
//     * @param map
//     * @param request
//     * @return
//     */
//    @PassToken
//    @ResponseBody
//    @RequestMapping("/getGoodsInfo")
//    public Response getGoodsInfo(@RequestParam Map<String, String> map, HttpServletRequest request) {
//
//        if (map != null) {
//            int userNoId = 0;
//            int rankId = 0;
//            if (map.get("userNoId") != null) {
//                userNoId = Integer.parseInt(map.get("userNoId"));
//            }
//
//            if (map.get("rankId") != null) {
//                rankId = Integer.parseInt(map.get("rankId"));
//            }
//
//
//            List<IndexGoodsRankData> indexGoodsRankDataList = null;
//            List<IndexGoodsHYToData> indexGoodsHYToDataList = null;
//            List<IndexGoodsDZToData> indexGoodsDZToDataList = null;
//
//            int pages = 0;
//            int limits = 10;
//
//            if (map.get("page") != null) {
//                pages = Integer.parseInt(map.get("page"));
//            }
//            if (map.get("limit") != null) {
//                limits = Integer.parseInt(map.get("limit"));
//            }
//
//
//            try {
//                if (userNoId == 0 || rankId <= 1) {//没有登录或rankId为游客
//                    System.out.println("游客：");
//                    PageHelper.startPage(pages, limits);
//                    indexGoodsRankDataList = wapGoodsAndSpecService.selectIndexGoodsByzoneIdAndRankId(map);
//
//                    if (indexGoodsRankDataList != null && indexGoodsRankDataList.size() != 0) {
//                        PageInfo<IndexGoodsRankData> pageInfo = new PageInfo<>(indexGoodsRankDataList);
//
//                        return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
//
//                    } else {
//                        indexGoodsRankDataList = new ArrayList<>();
//                        return new Response(MessageConstants.MSG_SUCCESS, indexGoodsRankDataList, 0, "", request);
//
//                    }
//
//                } else if (rankId > 1 && rankId <= 2) {//登录，rankId为会员
//                    System.out.println("会员：");
//                    PageHelper.startPage(pages, limits);
//                    indexGoodsHYToDataList = wapGoodsAndSpecService.selectIndexGoodsByzoneIAndHY(map);
//
//                    if (indexGoodsHYToDataList != null && indexGoodsHYToDataList.size() != 0) {
//
//                        PageInfo<IndexGoodsHYToData> pageInfo = new PageInfo<>(indexGoodsHYToDataList);
//
//                        return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
//
//                    } else {
//                        indexGoodsHYToDataList = new ArrayList<>();
//                        return new Response(MessageConstants.MSG_SUCCESS, indexGoodsHYToDataList, 0, "", request);
//
//                    }
//
//                } else if (rankId > 2 && rankId < 7) {
//                    System.out.println("店主：");
//                    switch (rankId) {
//                        case 3:
//                            PageHelper.startPage(pages, limits);
//                            indexGoodsDZToDataList = wapGoodsAndSpecService.selectIndexGoodsByZoneIdAndDZ1(map);
//                            System.out.println("店主3：");
//                            if (indexGoodsDZToDataList != null && indexGoodsDZToDataList.size() != 0) {
//                                PageInfo<IndexGoodsDZToData> pageInfo = new PageInfo<>(indexGoodsDZToDataList);
//                                return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
//
//                            } else {
//                                indexGoodsDZToDataList = new ArrayList<>();
//                                return new Response(MessageConstants.MSG_SUCCESS, indexGoodsDZToDataList, 0, "", request);
//
//                            }
//
//                        case 4:
//                            PageHelper.startPage(pages, limits);
//                            indexGoodsDZToDataList = wapGoodsAndSpecService.selectIndexGoodsByZoneIdAndDZ2(map);
//                            System.out.println("店主4：");
//                            if (indexGoodsDZToDataList != null && indexGoodsDZToDataList.size() != 0) {
//                                PageInfo<IndexGoodsDZToData> pageInfo = new PageInfo<>(indexGoodsDZToDataList);
//                                return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
//
//                            } else {
//                                indexGoodsDZToDataList = new ArrayList<>();
//                                return new Response(MessageConstants.MSG_SUCCESS, indexGoodsDZToDataList, 0, "", request);
//
//                            }
//
//
//                        case 5:
//                            PageHelper.startPage(pages, limits);
//                            indexGoodsDZToDataList = wapGoodsAndSpecService.selectIndexGoodsByZoneIdAndDZ3(map);
//                            System.out.println("店主5：" + indexGoodsDZToDataList.size());
//                            if (indexGoodsDZToDataList != null && indexGoodsDZToDataList.size() != 0) {
//                                PageInfo<IndexGoodsDZToData> pageInfo = new PageInfo<>(indexGoodsDZToDataList);
//                                return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
//
//                            } else {
//                                indexGoodsDZToDataList = new ArrayList<>();
//                                return new Response(MessageConstants.MSG_SUCCESS, indexGoodsDZToDataList, 0, "", request);
//
//                            }
//                        case 6:
//                            PageHelper.startPage(pages, limits);
//                            indexGoodsDZToDataList = wapGoodsAndSpecService.selectIndexGoodsByZoneIdAndDZ4(map);
//                            if (indexGoodsDZToDataList != null && indexGoodsDZToDataList.size() != 0) {
//                                PageInfo<IndexGoodsDZToData> pageInfo = new PageInfo<>(indexGoodsDZToDataList);
//                                return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
//
//                            } else {
//                                indexGoodsDZToDataList = new ArrayList<>();
//                                return new Response(MessageConstants.MSG_SUCCESS, indexGoodsDZToDataList, 0, "", request);
//
//                            }
//
//                        default:
//
//                            return new Response(MessageConstants.MSG_SUCCESS, null, 0, "", request);
//                    }
//
//
//                } else {
//                    return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
//                }
////
//
//
//            } catch (Exception e) {
//
//                e.printStackTrace();
//                return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
//            }
//
//
//        } else {
//            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
//        }
//
//    }


    /**
     * 根据首页商品专区查询商品
     *
     * @param map
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getGoodsInfo")
    public Response getGoodsInfo(@RequestParam Map<String, String> map, HttpServletRequest request) {

        if (map != null) {
            int userNoId = 0;
            int rankId = 0;
            int pages = 0;
            int limits = 10;
            int zoneId = 0;
            String token=request.getHeader("token");

            if (map.get("page") == null || map.get("zoneId") == null || map.get("limit") == null ) {
                return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
            }

            try {
                if(token != null&&token!=""){
                    userNoId=TokenUtils.getUserNoId(token);
                    if(userNoId!=0){
                        String url = HttpRequestConfig.userServerUrl+"/wap/getUserNoIdInfoByUserNoId";
                        String noId = "userNoId=" + userNoId;
                        String result = HttpRequest.sendGet(url, noId);
                        if(result!=null){
                            JSONObject str = JSONObject.parseObject(result);
                            JSONObject strData = JSONObject.parseObject(str.get("data").toString());
                            rankId=(Integer) strData.get("rankId");
                        }
                    }
                }

                pages = Integer.parseInt(map.get("page"));
                zoneId = Integer.parseInt(map.get("zoneId"));
                limits = Integer.parseInt(map.get("limit"));

            } catch (NumberFormatException e) {
                e.printStackTrace();
                return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
            }
            List<IndexGoods> indexGoodsList = null;
            //缓存获取数据
           Object object= redisTemplate.opsForValue().get("zoneId"+zoneId+"_"+pages);
           if(object!=null){
               indexGoodsList=(List<IndexGoods> )object;

           }else{
               PageHelper.startPage(pages, limits);
               indexGoodsList = wapGoodsAndSpecService.getIndexGoods(zoneId);
           }


            if (indexGoodsList == null || indexGoodsList.size() == 0) {//没有数据
                indexGoodsList = new ArrayList<>();
                return new Response(MessageConstants.MSG_SUCCESS, indexGoodsList, 0, "", request);
            }
            PageInfo<IndexGoods> pageInfo = new PageInfo<>(indexGoodsList);
            for (IndexGoods indexGoods : indexGoodsList) {
                switch (rankId) {
                    case 0:
                        indexGoods.setOwner(0.00);
                        break;
                    case 1:
                        indexGoods.setOwner(0.00);
                        break;
                    case 2:
                        indexGoods.setOwner(0.00);
                        break;
                    case 3:
                        indexGoods.setOwner(indexGoods.getOwner1());
                        break;
                    case 4:
                        indexGoods.setOwner(indexGoods.getOwner2());
                        break;
                    case 5:
                        indexGoods.setOwner(indexGoods.getOwner3());
                        break;
                    case 6:
                        indexGoods.setOwner(indexGoods.getOwner4());
                        break;
                    case 7:
                        indexGoods.setOwner(0.00);
                        break;
                    default:
                        indexGoods.setOwner(0.00);
                        break;
                }
            }
            long count=0;
            if(pageInfo.getPageSize()!=0){
                count=pageInfo.getTotal()/pageInfo.getPageSize()==0?(pageInfo.getTotal()/pageInfo.getPageSize()):((pageInfo.getTotal()/pageInfo.getPageSize())+1);
            }
            return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), count, "", request);

        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }


    }

    @ResponseBody
    @RequestMapping("/getGoodsInfoAttr")
    public Response getGoodsInfoAttr(@RequestParam Map<String, String> map, HttpServletRequest request) {

        if (map != null) {
            int userNoId = 0;
            int rankId = 0;
            int pages = 0;
            int limits = 10;
            String token=request.getHeader("token");

            if (map.get("page") == null || map.get("limit") == null ) {
                return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
            }

            try {
                if(token != null&&token!=""){
                    userNoId=TokenUtils.getUserNoId(token);
                    if(userNoId!=0){
                        String url = HttpRequestConfig.userServerUrl+"/wap/getUserNoIdInfoByUserNoId";
                        String noId = "userNoId=" + userNoId;
                        String result = HttpRequest.sendGet(url, noId);
                        if(result!=null){
                            JSONObject str = JSONObject.parseObject(result);
                            JSONObject strData = JSONObject.parseObject(str.get("data").toString());
                            rankId=(Integer) strData.get("rankId");
                        }
                    }
                }

                pages = Integer.parseInt(map.get("page"));
                limits = Integer.parseInt(map.get("limit"));

            } catch (NumberFormatException e) {
                e.printStackTrace();
                return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
            }
            List<IndexGoods> indexGoodsList = null;
            //缓存获取数据
            Object object= redisTemplate.opsForValue().get("IndexAttr0"+"_"+pages);
            if(object!=null){
                indexGoodsList=(List<IndexGoods> )object;

            }else{
                PageHelper.startPage(pages, limits);
                indexGoodsList = wapGoodsAndSpecService.getIndexGoodsAttr();
            }


            if (indexGoodsList == null || indexGoodsList.size() == 0) {//没有数据
                indexGoodsList = new ArrayList<>();
                return new Response(MessageConstants.MSG_SUCCESS, indexGoodsList, 0, "", request);
            }
            PageInfo<IndexGoods> pageInfo = new PageInfo<>(indexGoodsList);
            for (IndexGoods indexGoods : indexGoodsList) {
                switch (rankId) {
                    case 0:
                        indexGoods.setOwner(0.00);
                        break;
                    case 1:
                        indexGoods.setOwner(0.00);
                        break;
                    case 2:
                        indexGoods.setOwner(0.00);
                        break;
                    case 3:
                        indexGoods.setOwner(indexGoods.getOwner1());
                        break;
                    case 4:
                        indexGoods.setOwner(indexGoods.getOwner2());
                        break;
                    case 5:
                        indexGoods.setOwner(indexGoods.getOwner3());
                        break;
                    case 6:
                        indexGoods.setOwner(indexGoods.getOwner4());
                        break;
                    case 7:
                        indexGoods.setOwner(0.00);
                        break;
                    default:
                        indexGoods.setOwner(0.00);
                        break;
                }
            }
            return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getPageSize(), "", request);

        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }


    }

    @ResponseBody
    @RequestMapping("/getGoodsInfoShop")
    public Response getGoodsInfoShop(@RequestParam Map<String, String> map, HttpServletRequest request) {

        if (map != null) {
            int userNoId = 0;
            int rankId = 0;
            int pages = 0;
            int limits = 10;
            String token=request.getHeader("token");

            if (map.get("page") == null || map.get("limit") == null ) {
                return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
            }

            try {
                if(token != null&&token!=""){
                    userNoId=TokenUtils.getUserNoId(token);
                    if(userNoId!=0){
                        String url = HttpRequestConfig.userServerUrl+"/wap/getUserNoIdInfoByUserNoId";
                        String noId = "userNoId=" + userNoId;
                        String result = HttpRequest.sendGet(url, noId);
                        if(result!=null){
                            JSONObject str = JSONObject.parseObject(result);
                            JSONObject strData = JSONObject.parseObject(str.get("data").toString());
                            rankId=(Integer) strData.get("rankId");
                        }
                    }
                }

                pages = Integer.parseInt(map.get("page"));
                limits = Integer.parseInt(map.get("limit"));

            } catch (NumberFormatException e) {
                e.printStackTrace();
                return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
            }
            List<IndexGoods> indexGoodsList = null;
            //缓存获取数据
            Object object= redisTemplate.opsForValue().get("IndexAttr0"+"_"+pages);
            if(object!=null){
                indexGoodsList=(List<IndexGoods> )object;

            }else{
                PageHelper.startPage(pages, limits);
                indexGoodsList = wapGoodsAndSpecService.getIndexGoodsShop();
            }


            if (indexGoodsList == null || indexGoodsList.size() == 0) {//没有数据
                indexGoodsList = new ArrayList<>();
                return new Response(MessageConstants.MSG_SUCCESS, indexGoodsList, 0, "", request);
            }
            PageInfo<IndexGoods> pageInfo = new PageInfo<>(indexGoodsList);
            for (IndexGoods indexGoods : indexGoodsList) {
                switch (rankId) {
                    case 0:
                        indexGoods.setOwner(0.00);
                        break;
                    case 1:
                        indexGoods.setOwner(0.00);
                        break;
                    case 2:
                        indexGoods.setOwner(0.00);
                        break;
                    case 3:
                        indexGoods.setOwner(indexGoods.getOwner1());
                        break;
                    case 4:
                        indexGoods.setOwner(indexGoods.getOwner2());
                        break;
                    case 5:
                        indexGoods.setOwner(indexGoods.getOwner3());
                        break;
                    case 6:
                        indexGoods.setOwner(indexGoods.getOwner4());
                        break;
                    case 7:
                        indexGoods.setOwner(0.00);
                        break;
                    default:
                        indexGoods.setOwner(0.00);
                        break;
                }
            }
            return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getPageSize(), "", request);

        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }


    }


    /**
     * 搜索商品
     *
     * @param name
     * @param request
     * @return
     */
    @PassToken
    @ResponseBody
    @RequestMapping("/searchGoodsByName")
    public Response searchGoodsByName(@RequestParam("name") String name, HttpServletRequest request) {
        if (name != null) {
            List<SearchGoods> searchGoodsList = null;
            try {
                searchGoodsList = wapGoodsAndSpecService.selectGoodsAndSpaceByGoodsName(name);
                if (searchGoodsList != null && searchGoodsList.size() != 0) {
                    return new Response(MessageConstants.MSG_SUCCESS, searchGoodsList, 0, "", request);

                } else {
                    return new Response(MessageConstants.MSG_REQUEST_STATE, null, 0, "", request);
                }

            } catch (Exception e) {
                e.printStackTrace();
                return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
            }

        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
    }


    /**
     * 收藏商品
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/collectGoods")
    public Response collectGoods(HttpServletRequest request) {
        String userNoIds = request.getParameter("userNoId");
        String goodsIds = request.getParameter("goodId");
        String specIds = request.getParameter("specId");
        int userNoId = 0;
        int goodId = 0;
        int specId = 0;

        if (StringUtil.isNumeric(userNoIds)) {
            userNoId = Integer.parseInt(userNoIds);
        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        if (StringUtil.isNumeric(goodsIds)) {
            goodId = Integer.parseInt(goodsIds);
        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        if (StringUtil.isNumeric(specIds)) {
            specId = Integer.parseInt(specIds);
        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }

        //查询
        GoodsCollect goodsCollect1 = new GoodsCollect();
        goodsCollect1.setUserNoId(userNoId);
        goodsCollect1.setCollectSign("1");
        goodsCollect1.setGoodsId(goodId);
        GoodsCollect goodsCollect2 = goodsCollectService.queryCollectGoodsByUserNoId(goodsCollect1);
        if (goodsCollect2 != null) {
            return new Response(MessageConstants.MSG_SUCCESS, goodsCollect2.getCollectSign(), 0, "已经收藏了！", request);
        }

        //收藏
        GoodsCollect goodsCollect = new GoodsCollect();
        goodsCollect.setGoodsId(goodId);
        goodsCollect.setSpecId(specId);
        goodsCollect.setUserNoId(userNoId);
        goodsCollect.setCollectTime(new Timestamp(System.currentTimeMillis()));
        goodsCollect.setCollectSign("1");

        int row = goodsCollectService.insertSelective(goodsCollect);
        if (row > 0) {
            return new Response(MessageConstants.MSG_SUCCESS, row, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_ADD_FAILED, null, 0, "", request);
        }


    }


    /**
     * 获取收藏商品列表
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/getCollectGoods")
    public Response getCollectGoods(HttpServletRequest request) {

        String userNoIds = request.getParameter("userNoId");
        int userNoId = 0;
        if (StringUtil.isNumeric(userNoIds)) {
            userNoId = Integer.parseInt(userNoIds);
        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }

        List<GoodsCollectInfo> goodsCollectInfoList = goodsCollectService.selectGoodsCollectList(userNoId);
        if (goodsCollectInfoList != null && goodsCollectInfoList.size() != 0) {
            return new Response(MessageConstants.MSG_SUCCESS, goodsCollectInfoList, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_SUCCESS, null, 0, "", request);
        }

    }

    /**
     * 删除收藏商品
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/delCollectGoods")
    public Response delCollectGoods(HttpServletRequest request) {
        String gcIds = request.getParameter("gcId");
        int gcId = 0;
        if (StringUtil.isNumeric(gcIds)) {
            gcId = Integer.parseInt(gcIds);
        }

        int row = goodsCollectService.deleteByPrimaryKey(gcId);
        if (row > 0) {
            return new Response(MessageConstants.MSG_SUCCESS, row, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_DELETE_FAILED, null, 0, "", request);
        }

    }


    /**
     * 查询收藏商品
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/queryCollectGoods")
    public Response queryCollectGoods(HttpServletRequest request) {

        String userNoIds = request.getParameter("userNoId");
        int userNoId = 0;
        int goodId = 0;
        if (StringUtil.isNumeric(userNoIds)) {
            userNoId = Integer.parseInt(userNoIds);
        }

        String goodIds = request.getParameter("goodId");
        if (StringUtil.isNumeric(goodIds)) {
            goodId = Integer.parseInt(goodIds);
        }
        GoodsCollect goodsCollect = new GoodsCollect();
        goodsCollect.setUserNoId(userNoId);
        goodsCollect.setCollectSign("1");
        goodsCollect.setGoodsId(goodId);

        GoodsCollect goodsCollect1 = goodsCollectService.queryCollectGoodsByUserNoId(goodsCollect);
        if (goodsCollect1 != null) {
            return new Response(MessageConstants.MSG_SUCCESS, goodsCollect1.getCollectSign(), 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_SUCCESS, 0, 0, "", request);
        }

    }

}
